package com.mego.tzadd.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.mego.tzadd.R;
import com.mego.tzadd.database.manager.SystemCenter;
import com.mego.tzadd.utils.HttpRequest;
import com.mego.tzadd.utils.ShortMessage;
import com.mego.tzadd.utils.Utils;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

public class WjmmActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "WjmmActivity";

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    public static Intent createIntent(Context context) {
        return new Intent(context, WjmmActivity.class);
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wjmm_activity);

        initView();
        initData();
        initEvent();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    public void onForwardClick(View v) {

    }

    private EditText edit_mobile;
    private EditText edit_mima;
    private EditText edit_mima2;
    private EditText edit_yzm;

    @Override
    public void initView() {
        ImageButton ivBaseTabReturn = (ImageButton) findViewById(R.id.ivBaseTabReturn);
        if (ivBaseTabReturn != null) {
            ivBaseTabReturn.setScaleType(ImageView.ScaleType.FIT_START);
        }

        edit_mobile = (EditText) findViewById(R.id.edit_mobile);
        edit_mima = (EditText) findViewById(R.id.edit_mima);
        edit_mima2 = (EditText) findViewById(R.id.edit_mima2);
        edit_yzm = (EditText) findViewById(R.id.edit_yzm);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        findViewById(R.id.tv_xgmm).setOnClickListener(this);
        findViewById(R.id.tv_sendyzm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_xgmm:
                onXgmm();
                break;
            case R.id.tv_sendyzm:
                onSendyzm();
                break;
        }
    }

    String _yzm = "";

    private void onSendyzm() {
        int t = SystemCenter.getInstance().getSendsmstimes();
        if (t >= 5) {
            showShortToast("您已超出每日的短信驗證碼發送數量，請明天再發送！");
            return;
        }
        
        String mobile = edit_mobile.getText().toString();
        if (mobile.length() < 9 || !mobile.startsWith("0")) {
            //0912336153   台湾手机号
            showShortToast("請輸入正確的手機號碼!");
            return;
        }
        _yzm = Utils.getYzm();

        doSendyzm(mobile, _yzm);
    }

    private void doSendyzm(String mobile, String yzm) {
        if (!mobile.startsWith("+886")) {
            mobile = "+886" + mobile.substring(1);
        }
        yzm = "驗證碼：" + yzm + " [台主小幫手]";
        showProgressDialog("正在发送....");
        try {
            HttpRequest.sendsms(mobile, yzm, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    dismissProgressDialog();
                    if (code == 1) {
                        showShortToast("发送成功");
                    } else {
                        showShortToast("发送失败");
                    }
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }

    }

    private void onXgmm() {
        String mobile = edit_mobile.getText().toString();
        String mima = edit_mima.getText().toString();
        String mima2 = edit_mima2.getText().toString();
        String yzm = edit_yzm.getText().toString();

        if (!yzm.equals(_yzm)) {
            showShortToast("驗證碼錯誤！");
            return;
        }

        if (mobile.isEmpty() || mima.isEmpty()) {
            showShortToast("请输入密碼！");
            return;
        }

        if (!mima.equals(mima2)) {
            showShortToast("兩次密碼不一樣！");
            return;
        }

        showProgressDialog(R.string.loading);
        try {
            HttpRequest.czmm(SystemCenter.getInstance().username, mima, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    dismissProgressDialog();
                    if (code == 1) {
                        showShortToast("密碼設置完成!");
                        finishCzmm();
                    } else {
                        showShortToast("密碼設置失败");
                    }
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }
    }

    private void finishCzmm() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}

